package com.gxnzd.communityelderly.controller;

import com.gxnzd.communityelderly.commom.OSS;
import com.gxnzd.communityelderly.commom.R;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Date;

/**
 * ClassName: CommonController
 * Package: com.gxnzd.communityelderly.controller
 * Description:
 *
 * @Author 胡海峰
 * @Create 2024/10/11 12:54
 * @Version 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("/common")
@Slf4j
public class CommonController {


    @Resource
    private OSS oss;

    /**
     * 图片上传
     * @param file
     * @return
     */
    @PostMapping("/CommunityUpload")
    public R<String> upload(@RequestParam("file") MultipartFile file){
        String filePath ="D:\\JAVA\\GraduationDesign\\Project\\community_elderly_web\\src\\assets\\communityImg";
        String programPath = "D:\\JAVA\\GraduationDesign\\Project\\community_small_program\\static\\communityImg";
        try {
            Date date = new Date();
            //新的文件名字
            String newFileName = date.getTime() + ".jpg";
            //封装上传文件位置的全路径
            File targetFile = new File(filePath, newFileName);
            File programFile = new File(programPath,newFileName);
            //把本地文件上传到封装上传文件位置的全路径
            file.transferTo(targetFile);
            Files.copy(targetFile.toPath(),programFile.toPath());
            String imgPath = "/src/assets/communityImg/" + newFileName;
            return R.success(imgPath);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 上传用户头像
     * @param file
     * @return
     */
    @PostMapping("/UserImgUpload")
    public R<String> userUpload(@RequestParam("file") MultipartFile file){
        String filePath = "D:\\JAVA\\GraduationDesign\\Project\\community_elderly_web\\src\\assets\\userImg";
        String programPath = "D:\\JAVA\\GraduationDesign\\Project\\community_small_program\\static\\user_avatar";
        try {
            Date date = new Date();
            //新的文件名字
            String newFileName = date.getTime() + ".jpg";
            //封装上传文件位置的全路径
            File targetFile = new File(filePath, newFileName);
            File programFile = new File(programPath,newFileName);
            //把本地文件上传到封装上传文件位置的全路径
            file.transferTo(targetFile);
            Files.copy(targetFile.toPath(),programFile.toPath());
            String imgPath = "/src/assets/userImg/" + newFileName;
            return R.success(imgPath);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/postImgUpload")
    public R postUploadImg(@RequestParam("file") MultipartFile file){
        String filePath = "D:\\JAVA\\GraduationDesign\\Project\\community_elderly_web\\src\\assets\\postImage";
        String programPath = "D:\\JAVA\\GraduationDesign\\Project\\community_small_program\\static\\postImage";
        try {
            Date date = new Date();
            //新的文件名字
            String newFileName = date.getTime() + ".jpg";
            //封装上传文件位置的全路径
            File targetFile = new File(filePath, newFileName);
            File programFile = new File(programPath,newFileName);
            //把本地文件上传到封装上传文件位置的全路径
            file.transferTo(targetFile);
            Files.copy(targetFile.toPath(),programFile.toPath());
            String imgPath = "/src/assets/postImage/" + newFileName;
            return R.success(imgPath);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 删除图片
     * @param file
     * @return
     */
    @PostMapping("/deleteImg")
    public R<String> deleteImg(@RequestParam("file") String file){
        String filePath ="D:\\JAVA\\GraduationDesign\\Project\\community_elderly_web";
        File targetFile = new File(filePath + file);
        if (targetFile.delete()) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }
    @PostMapping("/uploadOss")
    public R<String> ossUploadImg(@RequestParam("file") MultipartFile file){
        Date date = new Date();
        //新的文件名字
        String newFileName = date.getTime() + ".jpg";
         return R.success(oss.uploadOss(file,newFileName));
    }
    @PostMapping("/deleteOss")
    public R ossDeleteImg(@RequestParam("file") String file){
        String[] urlList=file.split("/");
        return oss.deletePhoto(urlList[urlList.length-1]);
    }
}
